package com.yc.sole.erp.controller;

import com.yc.sole.common.domain.BaseIndexResp;
import com.yc.sole.erp.biz.CustomerBiz;
import com.yc.sole.erp.request.CustomerQueryReq;
import com.yc.sole.erp.request.CustomerReq;
import com.yc.sole.erp.response.CustomerResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 客户管理
 *
 * @author yizuomin
 * @date 2023-05-07
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerController {

    private final CustomerBiz customerBiz;

    /**
     * 新增客户表
     *
     * @param customerReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:customer:save", description = "新增客户")
    @PostMapping("/api/base-data/customer/save")
    public Result<Void> saveCustomer(@RequestBody @Validated(SaveGroup.class) CustomerReq customerReq) {
        customerBiz.saveCustomer(customerReq);
        return Result.success();
    }

    /**
     * 通过Id修改客户表
     *
     * @param customerReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:customer:update", description = "修改客户")
    @PostMapping("/api/base-data/customer/update")
    public Result<Void> updateCustomerById(@RequestBody @Validated(UpdateGroup.class) CustomerReq customerReq) {
        customerBiz.updateCustomerById(customerReq);
        return Result.success();
    }

    /**
     * 通过Id查询客户
     *
     * @param customerId
     * @return RestResult
     */
    @PreAuth(value = "base-data:customer:select", description = "通过Id查询客户")
    @PostMapping("/api/base-data/customer/{customerId}")
    public Result<CustomerResp> selectCustomerById(@PathVariable Long customerId) {
        return Result.data(customerBiz.selectCustomerById(customerId));
    }

    /**
     * 查询带索引的客户信息
     *
     * @return Result
     */
    @PreAuth(value = "base-data:customer:select", description = "查询带索引的客户信息")
    @PostMapping("/api/base-data/customer/index")
    public Result<List<BaseIndexResp<CustomerResp>>> selectAllCustomerWithIndex(@RequestBody CustomerQueryReq queryReq) {
        return Result.data(customerBiz.selectAllCustomerWithIndex(queryReq));
    }

    /**
     * 通过id删除客户表
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:customer:delete", description = "删除客户")
    @PostMapping("/api/base-data/customer/delete")
    public Result<Void> deleteCustomer(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        customerBiz.deleteCustomer(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 分页查询客户表
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "base-data:customer:select", description = "查询客户")
    @PostMapping("/api/base-data/customer/page")
    public Result<Paging<CustomerResp>> selectCustomerPage(@RequestBody @Validated BasePageReq<CustomerQueryReq> basePageReq) {
        return Result.data(customerBiz.selectCustomerPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 通过名字查询客户列表
     *
     * @param customerPageReq
     * @return Result
     */
    @PreAuth(value = "base-data:customer:select", description = "查询客户列表")
    @PostMapping("/api/base-data/customer/list")
    public Result<List<CustomerResp>> selectCustomerByName(@RequestBody @Validated CustomerQueryReq customerPageReq) {
        return Result.data(customerBiz.selectCustomerByName(customerPageReq.getCustomerName()));
    }
}
